import { setToken, removeToken, setUid, removeUid } from '@/utils/auth'
export const api = {
  //* 手机号登录
  login({ commit, dispatch }, params) {
    return new Promise((resolve, reject) => {
      this.$axios
        .$get('/login/cellphone', { params })
        .then((res) => {
          const token = res.token
          const uid = res.profile.userId
          setToken(token)
          setUid(uid)
          commit('SET_TOKEN', token)
          commit('SET_UID', uid)
          dispatch('getInfo')
          resolve()
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  //* 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      this.$axios
        .$get('/user/detail', {
          params: {
            uid: state.uid
          }
        })
        .then((res) => {
          if (res.code !== 200) {
            reject(new Error('验证失败，请重新登录！'))
          }
          commit('SET_NAME', res.profile.nickname)
          commit('SET_AVATAR', res.profile.avatarUrl)
          commit('set_UserInfo', res)
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  //* 用户退出
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      this.$axios
        .$get('/logout')
        .then(() => {
          removeUid()
          removeToken()
          commit('Reset_State')
          resolve()
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  //* 首页banner
  async homeBanner({ commit }, params) {
    return await this.$axios.$get('/banner')
  },
  //* 首页 热门推荐 传入拉取条数
  async hotPush({ commit }, params) {
    return await this.$axios.$get('personalized', { params })
  },
  //* 首页 热门推荐 的 标签
  async hotPush_tags({ commit }, params) {
    return await this.$axios.$get('/playlist/hot')
  },
  //* 首页新碟上架
  async newEst({ commit }, params) {
    return await this.$axios.$get('/album/newest')
  },
  //* 专辑内容 传入id
  async album(vuex, id) {
    return await this.$axios.$get('/album', {
      params: {
        id
      }
    })
  },
  //* 专辑动态信息  可获得专辑动态信息,如是否收藏,收藏数,评论数,分享数
  async dynamic(vuex, id) {
    return await this.$axios.$get('/album/detail/dynamic', {
      params: {
        id
      }
    })
  },
  //* 获取音乐详情  可以获取多个
  async getMusicDetail(vuex, ids) {
    return await this.$axios.$get('/song/detail', {
      params: {
        ids
      }
    })
  }
}
